<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class ServicePack_Model extends CI_Model{
	protected $_name = "service_pack";
	protected $_primary = "SpId";
	var $NameService_vn = "NameService_vn", $NameService_en = "NameService_en", $Sumary_vn = "Sumary_vn", $Sumary_en = "Sumary_en", $Description_vn = "Description_vn", $Description_en = "Description_en", $Price = "Price", $Duration = "Duration", $Image = "Image", $Status = "Status";
	
	function countServicePack(){
		$this->db->where($this->Status, 1);
		$this->db->or_where($this->Status, 2);
		$query = $this->db->get($this->_name);
		return $query->num_rows();
	}
	/*-------------<<<<<<<<<<<<<<<<<----------------------------*/
	function getServicePackAll(){
		$this->db->where($this->Status, 1);
		$query = $this->db->get($this->_name);
		return $query->result();
		
	}
	function getServicePack($spid){
		$this->db->where($this->_primary, $spid);
		$query = $this->db->get($this->_name);
		return $query->row();
		
	}
	function getServicePackDetailAll($spid){
		$this->db->where($this->_primary, $spid);
		$query = $this->db->get("service_pack_detail");
		return $query->result();
		
	}
	function getServicePackDetail($spdid){
		$this->db->join('service_pack_detail', 'service_pack_detail.SpId = service_pack.SpId');
		$this->db->where("service_pack_detail.SpdId", $spdid);
		$query = $this->db->get("service_pack");
		//print_r($query->result()); die;
		return $query->row();
	}
	/*-------------->>>>>>>>>>>>>>>>>>>---------------------------*/
	function getServicePackDetailBySpId($SpId){
		$this->db->where($this->_primary, $SpId);
		$query = $this->db->get("service_pack_detail");
		return $query->result();
	}
	
	function getServicePackLimit($currentPage, $number){
		$where = "(".$this->Status."=1 OR ".$this->Status."=2)";
		$this->db->where($where);
		$this->db->limit($number, $currentPage);
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$i = $currentPage;
			$j = 0;
			foreach($query->result() as $row){
				$data[$j] = $row;
				$i++;
				$data[$j]->Detail = $this->getServicePackDetailBySpId($data[$j]->SpId);
				$data[$j]->stt = $i;
				$j++;
			}
			return $data;
		}
		return false;
	}
	
	function searchServicePack($keysearch){
		$this->db->like($this->NameService_vn, $keysearch);	
		$where = "(".$this->Status."=1 OR ".$this->Status."=2)";
		$this->db->where($where);
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$i = 0;
			$j = 0;
			foreach($query->result() as $row){
				$data[$j] = $row;
				$i++;
				$data[$j]->stt = $i;
				$j++;
			}
			return $data;
		}
		return false;
	}
	function getListChannel($id){
		$this->db->join("service_pack_channel as spc","spc.SpId = $this->_name.SpId");
		$this->db->join("channel","channel.ChannelId = spc.ChannelId");
		$this->db->where("spc.SpId",$id);
		$this->db->where("channel.".$this->Status,1);
		$data = $this->db->get($this->_name);
		return $data->result();
	}
	function insertServicePack($urlImage){
		$this->db->set($this->NameService_vn, trim($this->input->post($this->NameService_vn, true)));
		$this->db->set($this->NameService_en, trim($this->input->post($this->NameService_en, true)));
		$this->db->set($this->Sumary_vn, trim($this->input->post($this->Sumary_vn, true)));
		$this->db->set($this->Sumary_en, trim($this->input->post($this->Sumary_en, true)));
		$this->db->set($this->Description_vn, trim($this->input->post($this->Description_vn)));
		$this->db->set($this->Description_en, trim($this->input->post($this->Description_en)));
		$this->db->set($this->Image, trim($urlImage));
		$this->db->set($this->Status, 1);
		$this->db->insert($this->_name);
		$id = $this->db->insert_id();
		return $id;
	}
	
	function updateServicePack($SpId, $urlImage){
		$data = array(
					$this->NameService_vn => trim($this->input->post($this->NameService_vn, true)),
					$this->NameService_en => trim($this->input->post($this->NameService_en, true)),
					$this->Sumary_vn => trim($this->input->post($this->Sumary_vn, true)),
					$this->Sumary_en => trim($this->input->post($this->Sumary_en, true)),
					$this->Description_vn => trim($this->input->post($this->Description_vn)),
					$this->Description_en => trim($this->input->post($this->Description_en)),
					$this->Image => trim($urlImage)
				);
		$this->db->where($this->_primary, $SpId);
		$this->db->update($this->_name, $data);
	}
	
	function changeStatusServicePack($SpId, $Status){
		try{
			$this->db->where($this->_primary, $SpId);
			if($Status == 1){
				$Status = 2;
			}elseif($Status == 2){
				$Status = 1;
			}
			$this->db->update($this->_name, array($this->Status => $Status));
			return $Status;
		}catch(Exception $ex){
			return false;
		}
	}
	
	function deleteServicePack($SpId){
		try{
			$this->db->where($this->_primary, $SpId);
			$this->db->update($this->_name, array($this->Status => 3));
			return true;
		}catch(Exception $ex){
			return false;
		}
	}
	
	function getServicePackBySpId($SpId){
		$this->db->where($this->_primary, $SpId);
		$where = "(".$this->Status."=1 OR ".$this->Status."=2)";
		$this->db->where($where);
		$query = $this->db->get($this->_name);
		return $query->row();
	}
	
}